<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>通过一个正则表达式分隔字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.preg-replace.html">« preg_replace</a></li>
      <li style="float: right;"><a href="book.ssdeep.html">ssdeep »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.pcre.html">PCRE 函数</a></li>
    <li>通过一个正则表达式分隔字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.preg-split" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_split</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">preg_split</span> &mdash; <span class="dc-title">通过一个正则表达式分隔字符串</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-split-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>preg_split</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$subject</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span><br>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
  通过一个正则表达式分隔给定字符串.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-split-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">pattern</code></dt>

     <dd>

      <p class="para">
       用于搜索的模式，字符串形式。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">subject</code></dt>

     <dd>

      <p class="para">
       输入字符串
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">limit</code></dt>

     <dd>

      <p class="para">
      如果指定，将限制分隔得到的子串最多只有<code class="parameter">limit</code>个，返回的最后一个
      子串将包含所有剩余部分。<code class="parameter">limit</code>值为-1， 0或null时都代表&quot;不限制&quot;。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">flags </code>可以是任何下面标记的组合(以位或运算 <code class="literal">|</code> 组合)：
       <dl>
        
         <dt>
<strong><code>PREG_SPLIT_NO_EMPTY</code></strong></dt>

         <dd>

          <span class="simpara">
          如果这个标记被设置， <span class="function"><strong>preg_split()</strong></span> 将仅返回分隔后的非空部分。
          </span>
         </dd>

        
        
         <dt>
<strong><code>PREG_SPLIT_DELIM_CAPTURE</code></strong></dt>

         <dd>

          <span class="simpara">
          如果这个标记设置了，用于分隔的模式中的括号表达式将被捕获并返回。
          </span>
         </dd>

        
        
         <dt>
<strong><code>PREG_SPLIT_OFFSET_CAPTURE</code></strong></dt>

         <dd>

          <p class="para">
          如果这个标记被设置, 对于每一个出现的匹配返回时将会附加字符串偏移量. 
          注意：这将会改变返回数组中的每一个元素, 使其每个元素成为一个由第<code class="literal">0</code>
          个元素为分隔后的子串，第<code class="literal">1</code>个元素为该子串在<code class="parameter">subject</code>
          中的偏移量组成的数组。
          </p>
         </dd>

        
       </dl>

      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-split-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  返回一个使用 <code class="parameter">pattern</code> 边界分隔 <code class="parameter">subject</code> 后得到
  的子串组成的数组， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.preg-split-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
如果传递的正则表达式无法正常解析，会发出 <strong><code>E_WARNING</code></strong>。 
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-split-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4745">
    <p><strong>示例 #1 <span class="function"><strong>preg_split()</strong></span>示例：获取搜索字符串的部分</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//使用逗号或空格(包含"&nbsp;",&nbsp;\r,&nbsp;\t,&nbsp;\n,&nbsp;\f)分隔短语<br /></span><span style="color: #0000BB">$keywords&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">preg_split</span><span style="color: #007700">(</span><span style="color: #DD0000">"/[\s,]+/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"hypertext&nbsp;language,&nbsp;programming"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$keywords</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div> 
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; hypertext
    [1] =&gt; language
    [2] =&gt; programming
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4746">
    <p><strong>示例 #2 将一个字符串分隔为组成它的字符</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'string'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$chars&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">preg_split</span><span style="color: #007700">(</span><span style="color: #DD0000">'//'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PREG_SPLIT_NO_EMPTY</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$chars</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; s
    [1] =&gt; t
    [2] =&gt; r
    [3] =&gt; i
    [4] =&gt; n
    [5] =&gt; g
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4747">
    <p><strong>示例 #3 分隔一个字符串并获取每部分的偏移量</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'hypertext&nbsp;language&nbsp;programming'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$chars&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">preg_split</span><span style="color: #007700">(</span><span style="color: #DD0000">'/&nbsp;/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PREG_SPLIT_OFFSET_CAPTURE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$chars</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; hypertext
            [1] =&gt; 0
        )

    [1] =&gt; Array
        (
            [0] =&gt; language
            [1] =&gt; 10
        )

    [2] =&gt; Array
        (
            [0] =&gt; programming
            [1] =&gt; 19
        )

)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.preg-split-notes">
  <h3 class="title">注释</h3>
  <div class="tip"><strong class="tip">小技巧</strong>
   <p class="para">
   如果你不需要正则表达式功能，可以有更快(并且更简单)的选择比如 <span class="function"><a href="function.explode.html" class="function">explode()</a></span>
    或 <span class="function"><a href="function.str-split.html" class="function">str_split()</a></span>。
   </p>
  </div>
  <div class="tip"><strong class="tip">小技巧</strong>
   <p class="para">
    如果没有成功匹配，将会返回一个数组，包含了单个元素，即输入的字符串。
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-split-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="pcre.pattern.html" class="link">PCRE 模式</a></li>
    <li class="member"><span class="function"><a href="function.preg-quote.html" class="function" rel="rdfs-seeAlso">preg_quote()</a> - 转义正则表达式字符</span></li>
    <li class="member"><span class="function"><a href="function.implode.html" class="function" rel="rdfs-seeAlso">implode()</a> - 用字符串连接数组元素</span></li>
    <li class="member"><span class="function"><a href="function.preg-match.html" class="function" rel="rdfs-seeAlso">preg_match()</a> - 执行匹配正则表达式</span></li>
    <li class="member"><span class="function"><a href="function.preg-match-all.html" class="function" rel="rdfs-seeAlso">preg_match_all()</a> - 执行一个全局正则表达式匹配</span></li>
    <li class="member"><span class="function"><a href="function.preg-replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a> - 执行一个正则表达式的搜索和替换</span></li>
    <li class="member"><span class="function"><a href="function.preg-last-error.html" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - 返回最后一个PCRE正则执行产生的错误代码</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>